package edu.uwosh.cs342.project4;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class PositionOverlay extends ItemizedOverlay<OverlayItem>{
	private OverlayItem chosenLocation;
	private String title = "";
	private String snippet = "";
	private Context context;
	public GeoPoint geoPoint;

	public PositionOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}//end constructor
	
	public PositionOverlay(Drawable defaultMarker, Context context){
		super(boundCenterBottom(defaultMarker));
		this.context = context;
	}//end constructor

	@Override
	protected OverlayItem createItem(int i) {
		return chosenLocation;
	}//end createItem

	@Override
	public int size() {
		return 1;
	}//end size
	
	public void setTitle(String title){
		this.title = title;
	}//end setTitle
	
	public void setSnippet(String snippet){
		this.snippet = snippet;
	}//end setSnippet
	
	public void setChosenLocation(GeoPoint chosenLocation){
		this.chosenLocation = new OverlayItem(chosenLocation, title, snippet);
		geoPoint = chosenLocation;
		populate();
	}//end setChosenLocation
	
	@Override
	protected boolean onTap(int index) {
		AlertDialog.Builder dialog = new AlertDialog.Builder(context);
		dialog.setTitle(chosenLocation.getTitle());
		dialog.setMessage(chosenLocation.getSnippet());
	  
		dialog.setNeutralButton("Add to Favorites", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
				DBHelper dbHelper = new DBHelper();
		        AsyncMethod<DBHelper> getBitmap = new AsyncMethod<DBHelper>(DBHelper.class, dbHelper, "validate");
		        getBitmap.execute();
		        
		        String data = (String)getBitmap.getResult();
		        String name = "Default Value";
		        
		        name = title;
		        
		        data = data + "," + geoPoint.getLatitudeE6() + "~" + geoPoint.getLongitudeE6()
		        		+"~"+ name + "~p";
		        AsyncMethod<DBHelper> sendStuff = new AsyncMethod<DBHelper>(DBHelper.class, dbHelper, "update");
		        sendStuff.execute(data);
			}});
	  
		dialog.setNegativeButton("Okay", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
			
			}});
	  
		dialog.show();
		return true;
	}//end onTap listener

}//end class
